#ifndef _RPOINTERSTACK_H_
#define _RPOINTERSTACK_H_

namespace Util
{
template <class T>
class RPointerStack
{
private:
	typedef struct _linkStack
	{
		T* item;
		_linkStack* pNext;
	} LinkStack;
public:
	T* Pop();
	void Push(T* item);
	T* Top();
	
public:
	RPointerStack() : iHead(NULL) {}
	virtual ~RPointerStack();

private:
	LinkStack* iHead;
};

template <class T>
RPointerStack<T>::~RPointerStack()
{
	T* pTmp = NULL;
	while((pTmp = Pop()) != NULL)
	{
		delete pTmp;
		pTmp = NULL;
	}
}

template <class T>
void RPointerStack<T>::Push(T* item)
{
	LinkStack* pLinkStack = new LinkStack;
	pLinkStack->item = item;
	pLinkStack->pNext = NULL;

	if(!iHead)
		iHead = pLinkStack;
	else
	{
		pLinkStack->pNext = iHead;
		iHead = pLinkStack;
	}
}

template <class T>
T* RPointerStack<T>::Top()
{
	if(!iHead)
		return NULL;

	return iHead->item;
}

template <class T>
T* RPointerStack<T>::Pop()
{
	if(!iHead)
		return NULL;

	LinkStack* ptmp = iHead;
	T* pRet = iHead->item;

	iHead = iHead->pNext;

	delete ptmp;
	ptmp = NULL;

	return pRet;
}
}
#endif